********************************************************************************
***	This do-file replicates the results in: 								****
*** Steiner, Nils D. & Harms, Philipp										****
***	Trade Shocks and the Nationalist Backlash in Political Attitudes:		****		
*** Panel Data Evidence from Great Britain									****
*** Journal of European Public Policy										****				
********************************************************************************

clear all
set more off
version 16.1

/*
*** How to reproduce our results
 1. Install ados used (fre, estout, polychoric, ivreg2) and graph schemes used (blindschemes), if necessary
		ssc install fre
		ssc install estout
		ssc install polychoric
		ssc install ivreg2
		ssc install blindschemes
		// for the code for Appendix C.1 (Rotemberg weights) users will need to install the bartik_weight.ado from https://github.com/paulgp/bartik-weight 
 2. Obtain data: The analysis in our paper uses data from the BHPS that interested parties will need to request from the UK Data Service directly:
	University of Essex. Institute for Social and Economic Research (2010) British Household Panel Survey: Waves 1-18, 1991-2009. [data collection]. 7th Edition. UK Data Service. SN: 5151, http://doi.org/10.5255/UKDA-SN-5151-1.
	University of Essex. Institute for Social and Economic Research. (2014) British Household Panel Survey, Waves 1-18, 1991-2009: Conditional Access, Local Authority Districts. [data collection]. 4th Edition. UK Data Service. SN: 6027, http://doi.org/10.5255/UKDA-SN-6027-2. 
3. Place all files in one working folder
		- all the BHPS datasets: in a subfolder named "\Data_BHPS" 
		- the macro-level datasets  (provided as part of our reproduction package): main folder
				-> "LAD_dataset.dta" "NUTS3level.dta" "Sector_imports.dta" "nuts3geo_coord.dta" "nuts3geo_data.dta"
		- the other do.files (provided as part of our reproduction package): main folder
4. Set this folder as the working directory
5. Run code below
6. Contact Nils Steiner (steiner@politik.uni-mainz.de) if any questions arise
*/

	
*** Process BHPS data
	* Combine the single BHPS datasets to one dataset
	do "Merge single BHPS datasets.do"
	save "BHPS_raw.dta", replace
	  
	* Recode the merged BHPS dataset
	do "Recode ind level BHPS.do"
	save "BHPS_recoded.dta", replace		

	
*** Merge macro data to processed indvidual-level BHPS data 
	* Merge local area level dataset to (recoded) BHPS data (includes NUTS 3 data)
	use "BHPS_recoded.dta", replace	
	merge m:1 lad13 year using "LAD_dataset.dta", gen(merge_LADtoBHPS)
	
		// drop LAD observations that could not be matched to BHPS individuals
		tab year if merge_LADtoBHPS==2
		drop if year>2008 & year<2017
	
		tab lad13nm if merge_LADtoBHPS==2
		list lad13nm year if merge_LADtoBHPS==2
		drop if merge_LADtoBHPS==2
	
		* check number of observed regions in final dataset
		codebook nuts3_2006 // 125 NUTS3 regions observed
		codebook nuts2_2006 // 36 NUTS2 regions observed
		codebook nuts1_2006 // 11 NUTS1 regions observed
			
		codebook lad13 // 405 LADs obverved		
	
	sort pid waven 
	
	// save final dataset 	
	save "BHPS_merged.dta", replace


*** Multilevel regressions: benchmark (Tables 2 & 3; Appendix C.2)	
	do "ML regressions benchmark.do"

	
*** Results for Appendix B: Descriptive results
	*** Analyze macro-level data
		* Sectoral import data: Plot change over time (Appendix B.1)
		do "Plot sectoral import data.do"
		
		* NUTS3-level data: Maps and scatterplots (Appendix B.2)
		 do "Plot NUTS3 level data.do"	
			
	*** Plots: Change in nationalist attitudes over time (Appendix B.3) 
		do "Plot change in nationalist attitudes.do"
	
	
*** Results for Appendix C: Additional results for nationalist backlash regressions
	*** Multilevel scatterplots: Random intercepts against China shock (Appendix C.1)
		do "ML scatterplots for nationalist attitudes.do"	
		
	*** Rotemberg weights (Appendix C.3)
		do "Rotemberg weights.do"
		
	*** Multilevel regressions: Benchmark with increase-per-worker measure of import shock (Appendix C.4)	
		do "ML regressions benchmark with incrperworker.do"

	*** Multilevel regressions: Results using alternatives measures of nationalist attitudes (Appendix C.5)	
		do "ML regressions alternative nationalism measures.do"

	*** (Multilevel) regressions: Robustness checks of baseline regressions (Appendix C.6)	
		do "ML regressions robustness checks of benchmark.do"
		
	*** Multilevel regressions: Including change in economic activity (Appendix C.7)	
		do "ML regressions change in local economy.do"

	*** Multilevel regressions: Regressions for EU support including change in nationalist attitudes (Appendix C.8)	
		do "ML regressions nationalism as mediator.do"

	*** Multilevel regressions: Individual heterogeneity (Appendix C.9)	
		do "ML regressions moderators.do"
		
	*** Multilevel regressions: Adding sectoral import growth at the employment level (Appendix C.10)	
		do "ML regressions sectoral import growth.do"

		
*** Results for Appendix D: Effects of the China shock on economic policy attitudes (Appendix D)
	do "ML regressions economic policy attitudes.do"